﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace VFDEmulator
{
    public partial class Form1 : Form
    {
        List<Char> chars;
        int currentChar = 0;
        bool connected = false;
        bool testMode = false;
        bool randomMode = false;
        Timer randomTimer;
        Random randomNumber;

        public Form1()
        {
            InitializeComponent();

            chars = new List<Char>();
            chars.Add(char1);
            chars.Add(char2);
            chars.Add(char3);
            chars.Add(char4);
            chars.Add(char5);
            chars.Add(char6);
            chars.Add(char7);
            chars.Add(char8);
            chars.Add(char9);
            chars.Add(char10);
            chars.Add(char11);
            chars.Add(char12);
            chars.Add(char13);
            chars.Add(char14);
            chars.Add(char15);
            chars.Add(char16);
            chars.Add(char17);
            chars.Add(char18);
            chars.Add(char19);
            chars.Add(char20);
            chars.Add(char21);
            chars.Add(char22);
            chars.Add(char23);
            chars.Add(char24);
            chars.Add(char25);
            chars.Add(char26);
            chars.Add(char27);
            chars.Add(char28);
            chars.Add(char29);
            chars.Add(char30);
            chars.Add(char31);
            chars.Add(char32);
            chars.Add(char33);
            chars.Add(char34);
            chars.Add(char35);
            chars.Add(char36);
            chars.Add(char37);
            chars.Add(char38);
            chars.Add(char39);
            chars.Add(char40);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //for (int i = 0; i < 40; i++)
            //{
            //    chars[i].Value = ' ';
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            connected = !connected;
            if (connected)
            {
                _stateButton.Text = "Disconnect";
                _stateLabel.Text = "Connected";
                _statePanel.BackColor = Color.Green;
                _comPort.Enabled = false;
                _baudRate.Enabled = false;
                Connect();
            }
            else
            {
                _stateButton.Text = "Connect";
                _stateLabel.Text = "Disconnected";
                _statePanel.BackColor = Color.DarkRed;
                _comPort.Enabled = true;
                _baudRate.Enabled = true;
                Disconnect();
            }
        }

        private void Connect()
        {
            _serialPort = new SerialPort(_comPort.Text, Convert.ToInt32(_baudRate.Text));
            _serialPort.ReceivedBytesThreshold = 8;
            _serialPort.Encoding = Encoding.Default;
            _serialPort.Open();
            _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
        }

        volatile bool  handlingData = false;
        void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!handlingData)
            {
                handlingData = true;
                if (e.EventType == SerialData.Chars)
                {
                    string rec = _serialPort.ReadExisting();

                    byte[] bytes = _serialPort.Encoding.GetBytes(rec);

                    bool position = false;
                    bool newChar = false;
                    int newCharCount = 0;
                    byte newCharIndex = 0;
                    List<byte> newCharLeds = new List<byte>();
                    foreach (byte b in bytes)
                    {
                        if (position)
                        {
                            currentChar = (int)b;
                            position = false;
                        }
                        else if (newChar)
                        {
                            if (newCharCount == 0)
                            {
                                Console.WriteLine("New Char: " + b);
                                newCharIndex = b;
                                newCharLeds = new List<byte>();
                            }
                            else if (newCharCount == 7)
                            {
                                newCharLeds.Add(b);
                                CharMemory.Instance.Memory.Add(newCharIndex, newCharLeds.ToArray());
                                newChar = false;
                            }
                            else
                            {
                                newCharLeds.Add(b);
                            }
                            newCharCount++;
                        }
                        else
                        {
                            switch (b)
                            {
                                case 26:
                                    newChar = true;
                                    break;
                                case 27:
                                    position = true;
                                    break;
                                default:
                                    Console.WriteLine(b);
                                    chars[currentChar].Value = b;
                                    currentChar++;
                                    if (currentChar == 40)
                                        currentChar = 0;
                                    break;
                            }
                        }
                    }
                }
                handlingData = false;
            }
        }

        private void Disconnect()
        {
            _serialPort.DataReceived -= new SerialDataReceivedEventHandler(_serialPort_DataReceived);
            _serialPort.Close();
        }

        private void WriteChar(char value)
        {
            //chars[currentChar].Value = value;
            currentChar++;
            if (currentChar == 40)
                currentChar = 0;
        }

        private void _testButton_Click(object sender, EventArgs e)
        {
            testMode = !testMode;
            if (testMode)
            {
                _testButton.Text = "Stop Test Mode";
                _line1Test.Enabled = true;
                _line2Test.Enabled = true;
            }
            else
            {
                _testButton.Text = "Start Test Mode";
                _line1Test.Enabled = false;
                _line2Test.Enabled = false;
            }
        }

        private void _line2Test_TextChanged(object sender, EventArgs e)
        {
            //for (int i = 0; i < 20; i++)
            //{
            //    if (i < _line2Test.Text.Length)
            //        chars[i + 20].Value = _line2Test.Text[i];
            //    else
            //        chars[i + 20].Value = ' ';
            //}
        }

        private void _line1Test_TextChanged(object sender, EventArgs e)
        {
            //for (int i = 0; i < 20; i++)
            //{
            //    if (i < _line1Test.Text.Length)
            //        chars[i].Value = _line1Test.Text[i];
            //    else
            //        chars[i].Value = ' ';
            //}
        }


        private void _randomButton_Click(object sender, EventArgs e)
        {
            randomMode = !randomMode;
            if (randomMode)
            {
                _randomButton.Text = "Stop Random Mode";
                _interval.Enabled = false;
                randomNumber = new Random();
                randomTimer = new Timer();
                randomTimer.Interval = Convert.ToInt32(_interval.Text);
                randomTimer.Tick += new EventHandler(randomTimer_Tick);
                randomTimer.Start();
            }
            else
            {
                _randomButton.Text = "Start Random Mode";
                _interval.Enabled = true;
                randomTimer.Stop();
                randomTimer.Tick -= new EventHandler(randomTimer_Tick);
                randomTimer = null;
            }
        }

        int current = 32;
        void randomTimer_Tick(object sender, EventArgs e)
        {
            if (currentChar == 65536)
                currentChar = 32;
            char c = Convert.ToChar(current++);
            _char.Text = c.ToString();
            WriteChar(c);
        }

    }
}
